﻿namespace Cmn
{
    using DBconnector.DB;
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;

    public class DB
    {
        public static string errorMessage = "";

        private static void doWriteLog(string _strSql, string _Msg)
        {
            Log.Write("错误的SQL语句：" + _strSql + "  错误信息：" + _Msg);
        }

        public static bool execSql(string strSql)
        {
            return execSql(WebConfig.getDefaultConn(), strSql);
        }

        public static bool execSql(string strSql, params SqlParameter[] commandParameters)
        {
            return execSql(WebConfig.getDefaultConn(), CommandType.Text, strSql, commandParameters);
        }

        public static bool execExist(string strSql, params SqlParameter[] commandParameters)
        {
            return execExist(WebConfig.getDefaultConn(), CommandType.Text, strSql, commandParameters);
        }

        public static bool execExist(string connStr, CommandType commandType, string strSql, params SqlParameter[] commandParameters)
        {
            return SqlHelper.ExecuteNonQuery(connStr, commandType, strSql, commandParameters) > 0;
        }

        public static bool execSql(string connStr, string strSql)
        {
            return execSql(connStr, CommandType.Text, strSql, null);
        }

        public static bool execSql(CommandType commandType, string strSql, params SqlParameter[] commandParameters)
        {
            return execSql(WebConfig.getDefaultConn(), commandType, strSql, commandParameters);
        }

        public static bool execSql(string connStr, CommandType commandType, string strSql, params SqlParameter[] commandParameters)
        {
            return SqlHelper.ExecuteNonQuery(connStr, commandType, strSql, commandParameters)>0;
        }

        public static bool execSqlTransaction(string strSql)
        {
            strSql = "begin transaction;" + strSql;
            strSql = strSql.TrimEnd(new char[] { ';' }) + ";if (@@error=0) commit else rollback;";
            return execSql(strSql);
        }

        public static DataSet getDataSet(string strSql)
        {
            return getDataSet(WebConfig.getDefaultConn(), strSql);
        }

        public static DataSet getDataSet(string strSql, params SqlParameter[] commandParameters)
        {
            return getDataSet(WebConfig.getDefaultConn(), CommandType.Text, strSql, commandParameters);
        }

        public static DataSet getDataSet(string connStr, string strSql)
        {
            return getDataSet(connStr, CommandType.Text, strSql, null);
        }

        public static DataSet getDataSet(CommandType commandType, string strSql, params SqlParameter[] commandParameters)
        {
            return getDataSet(WebConfig.getDefaultConn(), commandType, strSql, commandParameters);
        }

        public static DataSet getDataSet(string connStr, CommandType commandType, string strSql, params SqlParameter[] commandParameters)
        {
            try
            {
                return SqlHelper.ExecuteDataset(connStr, commandType, strSql, commandParameters);
            }
            catch (Exception exception)
            {
                errorMessage = exception.Message;
                doWriteLog(strSql, errorMessage);
                return null;
            }
        }

        public static DataTable getDataTable(string strSql)
        {
            return getDataTable(WebConfig.getDefaultConn(), strSql);
        }

        public static DataTable getDataTable(string connStr, string strSql)
        {
            return getDataTable(connStr, CommandType.Text, strSql, null);
        }

        public static DataTable getDataTable(string strSql, params SqlParameter[] commandParameters)
        {
            return getDataTable(WebConfig.getDefaultConn(), CommandType.Text, strSql, commandParameters);
        }

        public static DataTable getDataTable(CommandType commandType, string strSql, params SqlParameter[] commandParameters)
        {
            return getDataTable(WebConfig.getDefaultConn(), commandType, strSql, commandParameters);
        }

        public static DataTable getDataTable(string connStr, CommandType commandType, string strSql, params SqlParameter[] commandParameters)
        {
            try
            {
                return SqlHelper.ExecuteDataTable(connStr, commandType, strSql, commandParameters);
            }
            catch (Exception exception)
            {
                doWriteLog(strSql, errorMessage);
                throw exception;
            }
        }

        public static string getFieldValue(string strSql)
        {
            return getFieldValue(WebConfig.getDefaultConn(), strSql);
        }

        public static string getFieldValue(string strSql, params SqlParameter[] commandParameters)
        {
            return getFieldValue(WebConfig.getDefaultConn(), CommandType.Text, strSql, commandParameters);
        }

        public static string getFieldValue(string connStr, string strSql)
        {
            return getFieldValue(connStr, CommandType.Text, strSql, null);
        }

        public static string getFieldValue(CommandType commandType, string strSql, params SqlParameter[] commandParameters)
        {
            return getFieldValue(WebConfig.getDefaultConn(), commandType, strSql, commandParameters);
        }

        public static string getFieldValue(string connStr, CommandType commandType, string strSql, params SqlParameter[] commandParameters)
        {
            string str = "";
            try
            {
                object obj2 = null;
                obj2 = SqlHelper.ExecuteScalar(connStr, commandType, strSql, commandParameters);
                str = (obj2 == null) ? "" : obj2.ToString();
            }
            catch (Exception exception)
            {
                doWriteLog(strSql, exception.ToString());
                throw exception;
            }
            return str;
        }

        public static SqlDataAdapter getSqlDataAdapter(string strSql)
        {
            return getSqlDataAdapter(WebConfig.getDefaultConn(), strSql);
        }

        public static SqlDataAdapter getSqlDataAdapter(string connStr, string strSql)
        {
            return getSqlDataAdapter(connStr, CommandType.Text, strSql, null);
        }

        public static SqlDataAdapter getSqlDataAdapter(string strSql, params SqlParameter[] commandParameters)
        {
            return getSqlDataAdapter(WebConfig.getDefaultConn(), CommandType.Text, strSql, commandParameters);
        }

        public static SqlDataAdapter getSqlDataAdapter(CommandType commandType, string strSql, params SqlParameter[] commandParameters)
        {
            return getSqlDataAdapter(WebConfig.getDefaultConn(), commandType, strSql, commandParameters);
        }

        public static SqlDataAdapter getSqlDataAdapter(string connStr, CommandType commandType, string strSql, params SqlParameter[] commandParameters)
        {
            try
            {
                return SqlHelper.DataAdapter(connStr, commandType, strSql, commandParameters);
            }
            catch (Exception exception)
            {
                errorMessage = exception.Message;
                doWriteLog(strSql, errorMessage);
                throw exception;
            }
        }

        public static SqlDataReader getSqlDataReader(string strSql)
        {
            return getSqlDataReader(WebConfig.getDefaultConn(), strSql);
        }

        public static SqlDataReader getSqlDataReader(string strSql, params SqlParameter[] commandParameters)
        {
            return getSqlDataReader(WebConfig.getDefaultConn(), CommandType.Text, strSql, commandParameters);
        }

        public static SqlDataReader getSqlDataReader(string connStr, string strSql)
        {
            return getSqlDataReader(connStr, CommandType.Text, strSql, null);
        }

        public static SqlDataReader getSqlDataReader(CommandType commandType, string strSql, params SqlParameter[] commandParameters)
        {
            return getSqlDataReader(WebConfig.getDefaultConn(), commandType, strSql, commandParameters);
        }

        public static SqlDataReader getSqlDataReader(string connStr, CommandType commandType, string strSql, params SqlParameter[] commandParameters)
        {
            try
            {
                return SqlHelper.ExecuteReader(connStr, commandType, strSql, commandParameters);
            }
            catch (Exception exception)
            {
                errorMessage = exception.Message;
                doWriteLog(strSql, errorMessage);
                return null;
            }
        }

        public static ArrayList getTableAllField(string tableName)
        {
            ArrayList list = new ArrayList();
            DataTable table = getDataTable("select top 0 * from " + tableName);
            if (table != null)
            {
                for (int i = 0; i <= (table.Columns.Count - 1); i++)
                {
                    list.Add(table.Columns[i].ColumnName.Trim().ToLower());
                }
                table.Dispose();
            }
            return list;
        }

        public static string getTableKey(string tableName)
        {
            return getTableAllField(tableName)[0].ToString();
        }

        public static SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
        {
            return SqlHelper.MakeInParam(ParamName, DbType, Size, Value);
        }

        public static SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
        {
            return SqlHelper.MakeOutParam(ParamName, DbType, Size);
        }

        public static SqlParameter SqlMakeInParam(string ParamName, object Value)
        {
            return SqlHelper.MakeInParam(ParamName, Value);
        }
    }
}

